﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using App.Data;
using App.Core.Extensions;
using App.Core.DB;

public partial class Pages_Public_AccountActivation : BasePage
{    
    protected void Page_Load(object sender, EventArgs e)
    {
        BindPageInfo();
        if (!IsPostBack)
        {
            ActivateAccount();
        }
    }
    protected void BindPageInfo()
    {
        Page.Title = WebUtil.GetPageTitle(h4Heading.InnerHtml);
    }
    protected void ActivateAccount()
    {
        int customerID = WebUtil.GetQueryStringInInt(AppConstants.QueryString.ID);
        String verificationKey = WebUtil.GetQueryStringInString(AppConstants.QueryString.KEY);
        try
        {
            System.Guid key = System.Guid.Parse(verificationKey);
            BtcHousingEntities context = new BtcHousingEntities();
            var customer = context.Customers.FirstOrDefault(C => C.ID == customerID && C.VerificationKey == key);
            if (customer == null)
                ShowInvalideRequestMessage();
            else
            {
                if (customer.IsApproved)
                    WebUtil.ShowMessageBox(divMessage, "Your account has already activated!", true);
                else
                {
                    customer.IsApproved = true;
                    customer.Modified = DateTime.Now;
                    context.SaveChanges();
                    //WebUtil.ShowMessageBox(divMessage, "Your account has activated successfully.", false);
                    pnlContainer.Visible = true;
                }
            }
        }
        catch{
            ShowInvalideRequestMessage();
        }
    }
    protected void ShowInvalideRequestMessage()
    {
        WebUtil.ShowMessageBox(divMessage, "Invalid request for activation.", true);
        pnlContainer.Visible = false;
    }
}